<?php
class AclLinkHelper extends AppHelper {
	var $helpers = array('Session');

    function check($path){
        // assuming that allow('controllers') grands access to all actions
        if($this->Session->check('Auth.Permissions.controllers') 
        && $this->Session->read('Auth.Permissions.controllers') === true){
            return true;
        }
        if($this->Session->check('Auth.Permissions'.$path)
        && $this->Session->read('Auth.Permissions'.$path) === true){
            return true;
        }

		$path = explode('.', $path);

        if($this->Session->check('Auth.Permissions.'.$path[0].'.controllers')
        && $this->Session->read('Auth.Permissions.'.$path[0].'.controllers') === true){
            return true;
        }

        return false;
    }

	function checkModuleCadastro(){
		if ($this->check('Areas.index') ||
			$this->check('Grupos.index') || $this->check('Funcoes.index') ||
			$this->check('Projetos.index') || $this->check('Funcionarios.index')) {

			return true;
		}

		return false;
	}
	
	function checkModuleAdministracao() {
		if ($this->check('Empresas.index') || $this->check('Perfis.index') || $this->check('Usuarios.index') ||
			$this->check('Alertas.index') || $this->check('AclManager.Acl')) {

			return true;
		}

		return false;
	}

	function checkModuleViagens(){
		if ($this->check('Viagens.index') || $this->check('Trechos.index') ||
			$this->check('Regras.index') || $this->check('BonusDescontoConversoes.index')) {

			return true;
		}

		return false;
	}

	function checkViagem($situacao, $acao){
        $acoesDiponiveisAgendada = array("reschedule", "authorize", "cancel");
		$acoesDiponiveisReagendada = array("reschedule", "authorize", "cancel");
        $acoesDiponiveisAutorizada = array("reschedule", "approve", "cancel", "back");
		$acoesDiponiveisAprovada = array("reschedule", "emit", "cancel", "back");
		$acoesDiponiveisEmitida = array("reschedule", "reissue", "finalize", "cancel", "back");
		$acoesDiponiveisReemitida = array("reschedule", "reissue", "finalize", "cancel", "back");
		$acoesDiponiveisFinalizada = array("cancel", "back");

		if ($situacao == "agendada") {
			return in_array($acao, $acoesDiponiveisAgendada);
		}
		
		if ($situacao == "reagendada") {
			return in_array($acao, $acoesDiponiveisReagendada);
		}

		if ($situacao == "autorizada") {
			return in_array($acao, $acoesDiponiveisAutorizada);
		}

		if ($situacao == "aprovada") {
			return in_array($acao, $acoesDiponiveisAprovada);
		}

		if ($situacao == "emitida") {
			return in_array($acao, $acoesDiponiveisEmitida);
		}

		if ($situacao == "reemitida") {
			return in_array($acao, $acoesDiponiveisReemitida);
		}

		if ($situacao == "finalizada") {
			return in_array($acao, $acoesDiponiveisFinalizada);
		}

		return false;
	}
}